package io.renren.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.PersonEntity;
import io.renren.modules.sys.service.PersonService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 经手人表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-22 14:29:36
 */
@RestController
@RequestMapping("sys/person")
public class PersonController {
    @Autowired
    private PersonService personService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:person:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = personService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/getPersonByIds")
    public R getPersonByIds(@RequestParam Map<String, Object> params){
        PageUtils page = personService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/findBy")
    public R findBy(@RequestParam Map<String, Object> params){
//        limit page
        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<PersonEntity> page = personService.selectPage(
                new Query<PersonEntity>(params).getPage(),
                new EntityWrapper<PersonEntity>()
                        .orderBy("Id")
        );

        PageUtils pageUtils = new PageUtils(page);

        return R.ok().put("total",pageUtils.getTotalCount()).put("rows",pageUtils.getList());
    }

    /**
     * 根据类型查询
     */
    @RequestMapping("/type")
//    @RequiresPermissions("sys:person:info")
    public R getPersonByType(@RequestParam("Type") String type){
        List<PersonEntity> personList = personService.selectPersonByType(type);

        return R.ok().put("personList", personList);
    }

    /**
     * 根据类型查询
     */
    @RequestMapping("/personByNumType")
//    @RequiresPermissions("sys:person:info")
    public JSONArray getPersonByNumType(@RequestParam("Type") String type){
        if (type.equals("1")) {
            type = "业务员";
        } else if (type.equals("2")) {
            type = "仓管员";
        } else if (type.equals("3")) {
            type = "财务员";
        }
        List<PersonEntity> personList = personService.selectPersonByType(type);
        JSONArray  json = JSONArray.parseArray(JSON.toJSONString(personList));
        return json;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:person:info")
    public R info(@PathVariable("id") Long id){
        PersonEntity person = personService.selectById(id);

        return R.ok().put("person", person);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:person:save")
    public R save(@RequestBody PersonEntity person){
        personService.insert(person);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:person:update")
    public R update(@RequestBody PersonEntity person){
        ValidatorUtils.validateEntity(person);
        personService.updateAllColumnById(person);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:person:delete")
    public R delete(@RequestBody Long[] ids){
        personService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/getBasicData")
    public R getBasicData(@RequestParam Map<String, Object> params){
        PageUtils page = personService.queryPage(params);

        return R.ok().put("total",page.getTotalCount()).put("rows",page.getList());
    }

}
